package com.taotao.web.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.taotao.common.service.HttpClientService;

@Service
public class IndexService {
    @Autowired
    private HttpClientService clientService;

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexService.class);

    @Autowired
    private PropertisService propertisService;

    /**
     * 获取大广告 http://manage.taotao.com/rest/api/content/query?categoryId=31&page=1&rows=6
     * 
     * @return String 大广告数据
     * @throws IOException
     * @throws JsonProcessingException
     */
    public String queryAd1() throws JsonProcessingException, IOException {
        String content = null;
        try {
            content = clientService.doGet(this.propertisService.TT_MANAGE_URL + this.propertisService.TT_MANAGE_AD1);
        } catch (Exception e) {
            e.printStackTrace();
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("调用远程结构失败：查询大广告数据!");
            }
        }
        if (StringUtils.isBlank(content)) {
            // 如果为空（出现异常，或者无返回结果）
            // 设置一些默认结果
            return null;
        }
        // 将结果转化成jackJson的node
        JsonNode jsonNode = PropertisService.MAPPER.readTree(content);
        // 获取tree中的rows,rows是一个集合
        ArrayNode rows = (ArrayNode) jsonNode.get("rows");
        // 保存结果的map
        List<Map<String, Object>> result = new ArrayList<>();
        for (JsonNode jn : rows) {
            Map<String, Object> adMap = new HashMap<String, Object>();
            adMap.put("srcB", jn.get("pic").asText());
            adMap.put("height", 240);
            adMap.put("width", 670);
            adMap.put("src", jn.get("pic").asText());
            adMap.put("alt", jn.get("title").asText());
            adMap.put("widthB", 550);
            adMap.put("href", jn.get("url").asText());
            adMap.put("heightB", 240);
            result.add(adMap);
        }
        // 将结果转化成json字符串,编程前台需要的结果
        String resultStr = PropertisService.MAPPER.writeValueAsString(result);
        return resultStr;
    }

   
    /**
     * 获取右上角的小广告 http://manage.taotao.com/rest/api/content/query?categoryId=39&page=1&rows=1
     * 
     * @return String 右上角小广告
     * @throws IOException
     * @throws JsonProcessingException
     */
    public String queryAd2() throws JsonProcessingException, IOException {
        String content = null;
        try {
            content = clientService.doGet(this.propertisService.TT_MANAGE_URL + this.propertisService.TT_MANAGE_AD2);
        } catch (Exception e) {
            e.printStackTrace();
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("调用远程结构失败：查询右上角小广告数据!");
            }
        }
        if (StringUtils.isBlank(content)) {
            // 如果为空（出现异常，或者无返回结果）
            // 设置一些默认结果
            return null;
        }
        // 将结果转化成jackJson的node
        JsonNode jsonNode = PropertisService.MAPPER.readTree(content);
        // 获取tree中的rows,rows是一个集合
        ArrayNode rows = (ArrayNode) jsonNode.get("rows");
        // 保存结果的map
        List<Map<String, Object>> result = new ArrayList<>();
        for (JsonNode jn : rows) {
            Map<String, Object> adMap = new HashMap<String, Object>();
            adMap.put("srcB", jn.get("pic").asText());
            adMap.put("height", 70);
            adMap.put("width", 310);
            adMap.put("src", jn.get("pic").asText());
            adMap.put("alt", jn.get("title").asText());
            adMap.put("widthB", 210);
            adMap.put("href", jn.get("url").asText());
            adMap.put("heightB", 70);
            result.add(adMap);
        }
        // 将结果转化成json字符串,编程前台需要的结果
        String resultStr = PropertisService.MAPPER.writeValueAsString(result);
        return resultStr;
    }

}
